package org.hackystat.ui.projectviewer.gwt.client;

import org.hackystat.ui.projectviewer.gwt.client.dataprovider.DataProviderMediator;

import com.google.gwt.junit.client.Benchmark;
import com.google.gwt.junit.client.Category;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * GWT JUnit tests must extend GWTTestCase.
 *
 * @author Pavel Senin.
 */
public class BenchmarkProjectViewer extends Benchmark {

  /**
   * @gwt.benchmark.name BencmarkProjectViewer
   * @gwt.benchmark.description A series of benchmarks that tests RPC
   *
   */
  class AllocCategory implements Category {
  }

  /**
   * {@inheritDoc}
   */
  public String getModuleName() {
    // addresses the module which will be used for test
    return "org.hackystat.ui.projectviewer.gwt.TestProjectViewer";
  }

  /**
   * Add as many tests as you like.
   *
   * @throws Exception if error encountered.
   */
  public void testStuff() throws Exception {
    // final Server sensorbaseServer = org.hackystat.sensorbase.server.Server.newTestInstance();
    // final String host = sensorbaseServer.getServerProperties().getFullHost();
    // Setup an asynchronous event handler.
    Timer timer = new Timer() {
      public void run() {
        // (1) Getting mediator instance
        //
        DataProviderMediator service = DataProviderMediator.getInstance();
        // (2) Create an asynchronous callback to handle the result.
        //
        AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
          public void onSuccess(Boolean success) {
            assertTrue("Testing confirmHost()", success);
            // tell the test system the test is now done
            finishTest();
          }

          public void onFailure(Throwable caught) {
            assert true;
            // do some UI stuff to show failure
          }
        };
        // (3) Make the call. Control flow will continue immediately and later
        // 'callback' will be invoked when the RPC completes.
        //
        service.confirmSensorbaseHost("http://localhost:15230/sensorbase", callback);
        // service.getService().confirmHost(host, callback);
      }
    };

    // Set a delay period significantly longer than the
    // event is expected to take.
    delayTestFinish(20000);

    // Schedule the event and return control to the test system.
    timer.schedule(1900);
  }

}
